﻿// -----------------------------------------------------------------------
// <copyright file="MetalComboBox.cs" company="Abyssalsoft">
// TODO: Update copyright text.
// </copyright>
// -----------------------------------------------------------------------

namespace Metal.Config.ExtendedControls
{
    using System;
    using System.Collections.Generic;
    using System.Text;

    /// <summary>
    /// Custom ComboBox
    /// </summary>
    public class MetalComboBox : System.Windows.Forms.ComboBox
    {
        /// <summary>Stores the attribute / setting name, where the value of the check box will be saved</summary>
        private string internalValue;

        /// <summary>Stores the resource ID</summary>
        private string strResourceID;

        private SaveProperty propertyType;

        private bool valueChanged;

        public MetalComboBox()
        {
            this.Click += new EventHandler(MetalComboBox_Click);
        }

        void MetalComboBox_Click(object sender, EventArgs e)
        {
            this.valueChanged = true;
        }

        public enum SaveProperty
        {
            Value,
            Index
        }

        /// <summary>
        /// Gets or sets the setting name
        /// </summary>
        public string SettingName
        {
            get
            {
                return this.internalValue;
            }

            set
            {
                this.internalValue = value;
            }
        }

        /// <summary>
        /// Gets or sets the resource ID
        /// </summary>
        public string ResourceID
        {
            get
            {
                return this.strResourceID;
            }

            set
            {
                this.strResourceID = value;
            }
        }

        public SaveProperty PropertyType
        {
            get
            {
                return this.propertyType;
            }

            set
            {
                this.propertyType = value;
            }
        }

        /// <summary>
        /// Save ComboBox selected value
        /// </summary>
        public void SaveSetting()
        {
            if (valueChanged)
            {
                if (PropertyType == SaveProperty.Index)
                {
                    Settings.Instance.SetValue("LastSettings." + this.SettingName, this.SelectedIndex.ToString());
                }
                else if (PropertyType == SaveProperty.Value)
                {
                    Settings.Instance.SetValue("LastSettings." + this.SettingName, this.Text);
                }
            }
        }

        /// <summary>
        /// When the control becomes visible load the control value (checked / unchecked) and the text (resource)
        /// </summary>
        /// <param name="e">Event args</param>
        protected override void OnVisibleChanged(EventArgs e)
        {
            base.OnVisibleChanged(e);

            if (!this.DesignMode)
            {
                if (this.SettingName != null)
                {
                    if (PropertyType == SaveProperty.Index)
                    {
                        this.SelectedIndex = Settings.Instance.GetValue("LastSettings." + this.SettingName, 0);
                    }
                    else if (PropertyType == SaveProperty.Value)
                    {
                        this.Text = Settings.Instance.GetValue("LastSettings." + this.SettingName, string.Empty);
                    }
                }
            }
        }

        /// <summary>
        /// Load combo values when the control is created. Those values are loaded from resource file
        /// </summary>
        protected override void OnCreateControl()
        {
            base.OnCreateControl();

            if (this.ResourceID != string.Empty)
            {
                string strResource = Resources.MessageProvider.GetMessage(this.ResourceID);

                this.Items.Clear();

                string[] rows = strResource.Split(';');
                foreach (string row in rows)
                {
                    this.Items.Add(row);
                }
            }
        }
    }
}
